var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var settingSchoolClassTree = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    },
    callback: {
        onClick: chooseClass
    }
};

var ztree;
var ztreeClass;

var zTreeSchoolClassTree;

function chooseClass(event, treeId, treeNode) {
   // alert(treeNode.id)
    vm.id=treeNode.id
    vm.reload()
}

$(function () {
    vm.getSchool();
    $("#jqGrid").jqGrid({
        url: baseURL + 'course/student/list?id='+vm.id,
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			// { label: '用户Id', name: 'userId', index: 'user_id', width: 80 },
			{ label: '学生姓名', name: 'name', index: 'name', width: 80 },
            { label: '手机号', name: 'phone', index: 'phone', width: 80 },
            { label: '学校名称', name: 'schoolName', width: 80},
            { label: '专业名称', name: 'professionName', width: 80},
            { label: '班级名称', name: 'schoolClassName', width: 80},
			{ label: '身份证', name: 'idCord', index: 'id_cord', width: 80 ,
                formatter: function (data) {
				if(data==null)
                    return "未输入";
				else
					return data;

                }},
			{ label: '学号', name: 'schoolCard', index: 'school_card', width: 80,
				formatter: function (data) {
                    if(data==null)
                        return "未采集";
                    else
                        return data;

                } },

			{ label: '高考分数', name: 'collegeExamScore', index: 'college_exam_score', width: 80,
                formatter: function (data) {
                    if(data==null)
                        return "未采集";
                    else
                        return data;

                } },
			// { label: '', name: 'createTime', index: 'create_time', width: 80 },

			{ label: '紧急联系人', name: 'emergencyContact', index: 'emergency_contact', width: 80,
                formatter: function (data) {
                    if(data==null)
                        return "未维护";
                    else
                        return data;

                } },
			{ label: '紧急联系电话', name: 'emergencyPhone', index: 'emergency_phone', width: 80,
                formatter: function (data) {
                    if(data==null)
                        return "未维护";
                    else
                        return data;

                } },
			{ label: '学生类型', name: 'learnClassName', index: 'learn_class_id', width: 80 }
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
	    id:null,
        searching:"",
        learnTypeSelectd:0,
        showList: true,
        title: null,
        schoolName:"",
		student: {
            schoolId :0,
            schoolName:null
		},
        learnType:[
            {
                id:1,
                name:'中兴班'
            },
            {
                id:2,
                name:'短训生'
            }
        ]
	},
	methods: {
        getSchool: function(){
            //加载部门树
            $.get(baseURL + "course/school/getSchoolTree", function(r){
                ztree = $.fn.zTree.init($("#schoolTree"), setting, r.deptList);
                var node = ztree.getNodeByParam("id", 0);
                console.log(node);
                ztree.selectNode(node);
            })

            $.get(baseURL + "course/school/select", function(r){
                ztreeClass = $.fn.zTree.init($("#schoolClassTree"), setting, r.deptList);
                var node = ztreeClass.getNodeByParam("id", 0);
                console.log(node);
                ztreeClass.selectNode(node);

                zTreeSchoolClassTree = $.fn.zTree.init($("#schoolClassTree1"), settingSchoolClassTree, r.deptList);
                var node = zTreeSchoolClassTree.getNodeByParam("id", 0);
                ztreeClass.selectNode(node);



            })
            $.get(baseURL + "course/student/getLearnTypeList", function(r){

            })

        },
        deptTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择学校",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.student.schoolId = node[0].id;
                    vm.student.schoolName = node[0].name;

                    layer.close(index);
                }
            });
        },
        deptTree1: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择班级",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#deptLayerClass"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztreeClass.getSelectedNodes();
                    //选择上级部门
                    vm.student.schoolClassId = node[0].id;
                    vm.student.schoolClassName = node[0].name;

                    layer.close(index);
                }
            });
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.student = {
                schoolId :0,
                schoolName:null,
                schoolClassId :0,
                schoolClassName:null,
                learnType:1
            };
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        validator:function () {
            if(isBlank(vm.student.name)){
                alert("姓名不能为空");
                return true;
            }
            if(!(/^1[34578]\d{9}$/.test(vm.student.phone))){
                alert("请输入正确手机号");
                return true;
            }else{
                //获取手机号是否存在
            }
            if(isBlank(vm.student.schoolName)){
                alert("请选择学校");
                return true;
            }
            if(isBlank(vm.student.schoolClassName)){
                alert("请选择班级");
                return true;
            }
        },
		saveOrUpdate: function (event) {
			var url = vm.student.id == null ? "course/student/save" : "course/student/update";

            if(vm.validator()){
                return ;
            }

			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.student),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "course/student/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "course/student/info/"+id, function(r){
                vm.student = r.student;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                url: baseURL + 'course/student/list?schoolId='+vm.id,
                postData:
                    {
                        'sea': vm.searching,
                        // 'learnType':vm.learnTypeSelectd
                    },
                page:page
            }).trigger("reloadGrid");
		}
	}
});
